package com.bfxy.rabbitmq.api.limit;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.QueueingConsumer;
import com.rabbitmq.client.QueueingConsumer.Delivery;

public class Receiver {

	public static void main(String[] args) throws Exception {

        Connection connection = null;
        Channel channel = null;
        try {
            ConnectionFactory connectionFactory = new ConnectionFactory() ;

            connectionFactory.setHost("192.168.218.21");
            connectionFactory.setPort(5672);
            connectionFactory.setVirtualHost("/");
            connectionFactory.setAutomaticRecoveryEnabled(true);
            connectionFactory.setNetworkRecoveryInterval(3000);

            connection = connectionFactory.newConnection();

            channel = connection.createChannel();

            String queueName = "test001";
            channel.queueDeclare(queueName, true, false, false, null);
            QueueingConsumer consumer = new QueueingConsumer(channel);
            // 消费端限流设置
            channel.basicQos(0, 1, false);
            //	参数：队列名称、是否自动ACK、Consumer
            channel.basicConsume(queueName, false, consumer);
            //	循环获取消息
            while (true) {
                //	获取消息，如果没有消息，这一步将会一直阻塞
                Delivery delivery = consumer.nextDelivery();
                String msg = new String(delivery.getBody());
                System.out.println("收到消息：" + msg);
                channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (channel != null) {
                channel.close();
            }
            if (connection != null) {
                connection.close();
            }
        }
	}
}
